package com.crm.controller;

import com.crm.pojo.BasDict;
import com.crm.pojo.SalChance;
import com.crm.pojo.SalPlan;
import com.crm.service.basDict.BasDictService;
import com.crm.service.salChance.SalChanceService;
import com.crm.service.salPlan.SalPlanService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/plan")
public class SalPlanController {
    @Resource
    private SalPlanService salPlanService;
    @Resource
    private SalChanceService salChanceService;
    @Resource
    private BasDictService basDictService;

    //当前端页面传过来的的String类型的日期与后台实体类的Date类型不匹配时，需要加上该方法
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping(value = "/list")
    public String list(
            @RequestParam(required = false) String chcCustName, @RequestParam(required = false) String chcLinkman,
            @RequestParam(required = false) String chcTitle, @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
            Model model
    ) throws Exception {
        Sort sort = Sort.by(Sort.Direction.ASC, "chcId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
        Map param = new HashMap();

        if (chcCustName != null && !chcCustName.equals(""))
            param.put("chcCustName", chcCustName);
        if (chcLinkman != null && !chcLinkman.equals(""))
            param.put("chcLinkman", chcLinkman);
        if (chcTitle != null && !chcTitle.equals(""))
            param.put("chcTitle", chcTitle);

        Page<SalChance> salChancePage = salChanceService.findSalPlanPageByMap(param, pageable);
        List<BasDict> basDictList = basDictService.findBasDictListByDictType();

        model.addAttribute("salChancePage", salChancePage);
        model.addAttribute("chcCustName", chcCustName);
        model.addAttribute("chcLinkman", chcLinkman);
        model.addAttribute("chcTitle", chcTitle);

        model.addAttribute("basDictList", basDictList);

        return "plan/list";
    }

    @RequestMapping(value = "/enact/{chcId}")
    public String enact(@PathVariable Long chcId, Model model) throws Exception {
        SalChance salChance = salChanceService.findSalChanceById(chcId);
        List<SalPlan> salPlanList = salPlanService.findSalPlansByChcId(chcId);

        model.addAttribute("salChance", salChance);
        model.addAttribute("salPlanList", salPlanList);

        return "plan/enact";
    }

    @ResponseBody
    @RequestMapping(value = "/edit/plaTodo/{plaId}/{plaTodo}")
    public Map editPlaTodo(@PathVariable Long plaId, @PathVariable String plaTodo) throws Exception {
        Map resultMap = new HashMap();

        if (salPlanService.editSalPlanInPlaTodoByPlaId(plaTodo, plaId) > 0) {
            resultMap.put("editResult", "true");
        } else {
            resultMap.put("editResult", "false");
        }

        return resultMap;
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Map add(Long plaChcId, String plaTodo) throws Exception {
        Map resultMap = new HashMap();
        SalPlan salPlan = new SalPlan();
        salPlan.setPlaChcId(plaChcId);
        salPlan.setPlaTodo(plaTodo);

        salPlan = salPlanService.addSlaPlan(salPlan);

        if (salPlan != null) {
            resultMap.put("addResult", "true");
            resultMap.put("addPlaId", salPlan.getPlaId());
        } else {
            resultMap.put("addResult", "false");
        }

        return resultMap;
    }

    @ResponseBody
    @RequestMapping(value = "/del/{plaId}")
    public Map del(@PathVariable Long plaId) throws Exception {
        Map resultMap = new HashMap();

        salPlanService.delSalPlanByPlaId(plaId);
        resultMap.put("delResult", "true");

        return resultMap;
    }

    @RequestMapping(value = "/execute/{chcId}")
    public String execute(@PathVariable Long chcId, Model model) throws Exception {
        SalChance salChance = salChanceService.findSalChanceById(chcId);
        List<SalPlan> salPlanList = salPlanService.findSalPlansByChcId(chcId);

        model.addAttribute("salChance", salChance);
        model.addAttribute("salPlanList", salPlanList);

        return "plan/execute";
    }

    @ResponseBody
    @RequestMapping(value = "/edit/plaResult/{plaId}/{plaResult}")
    public Map editPlaResult(@PathVariable Long plaId, @PathVariable String plaResult) throws Exception {
        Map resultMap = new HashMap();

        if (salPlanService.editSalPlanInPlaResultByPlaId(plaResult, plaId) > 0) {
            resultMap.put("editResult", "true");
        } else {
            resultMap.put("editResult", "false");
        }

        return resultMap;
    }
}